<?php
  //Loader::loadFile('config.php', 'modules/VoteThailand');
  /**
  * initialise module
  *
  */
  function VoteData_init() {

    //Create votedata_province table
    if (!DBUtil::createTable('votedata_province')) {
        return false;
    }
    //Create votedata_district table
    if (!DBUtil::createTable('votedata_district')) {
        return false;
    }
    //Create votedata_province table
    if (!DBUtil::createTable('votedata_tambon')) {
        return false;
    }
    //Create votedata_village table
    if (!DBUtil::createTable('votedata_village')) {
        return false;
    }

    //Create votedata_house_no table
    if (!DBUtil::createTable('votedata_house_no')) {
        return false;
    }

    //Create votedata_gender table
    if (!DBUtil::createTable('votedata_gender')) {
        return false;
    }

    //Create votedata_person table
    if (!DBUtil::createTable('votedata_person')) {
        return false;
    }

    //Create votedata_party table
    if (!DBUtil::createTable('votedata_party')) {
        return false;
    }

    //Create votedata_candidate table
    if (!DBUtil::createTable('votedata_candidate')) {
        return false;
    }

    //Create votedata_person_party table
    if (!DBUtil::createTable('votedata_person_party')) {
        return false;
    }

    //Create votedata_person_leader_canvasser table
    if (!DBUtil::createTable('votedata_person_leader_canvasser')) {
        return false;
    }

    //Create votedata_person_canvasser table
    if (!DBUtil::createTable('votedata_person_canvasser')) {
        return false;
    }

    //Create votedatacenter_person_canvasser table
    if (!DBUtil::createTable('votedata_migrate')) {
        return false;
    }

    //Create votedatacenter_person_canvasser table
    if (!DBUtil::createTable('votedata_person_temp')) {
        return false;
    }

    VoteData_init_data();
    VoteData_init_folder();
    return true;
  }


  function VoteData_init_folder(){

    if (!is_dir("pnTemp/votedata_upload/")) {
      mkdir("pnTemp/votedata_upload/", 0755);
    }
    
  }

  function VoteData_delete() {

    DBUtil::dropTable('votedata_province');
    DBUtil::dropTable('votedata_district');
    DBUtil::dropTable('votedata_tambon');
    DBUtil::dropTable('votedata_village');
    DBUtil::dropTable('votedata_house_no');
    DBUtil::dropTable('votedata_gender');
    DBUtil::dropTable('votedata_person');
    DBUtil::dropTable('votedata_party');
    DBUtil::dropTable('votedata_candidate');
    DBUtil::dropTable('votedata_person_party');
    DBUtil::dropTable('votedata_person_leader_canvasser');
    DBUtil::dropTable('votedata_person_canvasser');
    DBUtil::dropTable('votedata_migrate');
    DBUtil::dropTable('votedata_person_temp');

/*
 DROP TABLE `zk_votedata_candidate`;
 DROP TABLE `zk_votedata_district`;
 DROP TABLE `zk_votedata_gender`;
 DROP TABLE `zk_votedata_house_no`;
 DROP TABLE `zk_votedata_party`;
 DROP TABLE `zk_votedata_person`;
 DROP TABLE `zk_votedata_person_canvasser`;
 DROP TABLE `zk_votedata_person_leader_canvasser`;
 DROP TABLE `zk_votedata_person_party`;
 DROP TABLE `zk_votedata_province`;
 DROP TABLE `zk_votedata_tambon`;
 DROP TABLE `zk_votedata_village`;
 DROP TABLE `zk_votedata_project`;
*/
    return true;
  }

   function VoteData_init_data(){

    //Init gender
    $genderArray = array(
                          array('id'        => '1',
                                'name'      => 'ชาย'),
                          array('id'        => '2',
                                'name'      => 'หญิง')
                        );
    DBUtil::insertObjectArray($genderArray, 'votedata_gender', true);
    unset($genderArray);
    unset($data);

    //Init province data
    $file = "modules/VoteData/data/province.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $objArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $provinceArray[] = array(
                                    'id'        => $data[0],
                                    'name'      => VoteData_fixEncoding($data[1]),
                                    'latitude'  => $data[2],
                                    'longitude' => $data[3]
                                  );
      }
      DBUtil::insertObjectArray($provinceArray, 'votedata_province', true);
      unset($provinceArray);
      unset($data);
      fclose($handle);
    }

    //Init district data
    $file = "modules/VoteData/data/district.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $districtArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $districtArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'name'      => $data[2],
                                      'latitude'  => $data[3],
                                      'longitude' => $data[4]
                                    );
      }
      //DBUtilEx::setCharacterSet('utf8');
      DBUtil::insertObjectArray($districtArray, 'votedata_district', true);
      unset($districtArray);
      unset($data);
      fclose($handle);
    }


    //Init tambon data
    $file = "modules/VoteData/data/tambon.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $tambonArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $tambonArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'name'      => $data[3],
                                      'latitude'  => $data[4],
                                      'longitude' => $data[5]
                                    );
      }
      DBUtil::insertObjectArray($tambonArray, 'votedata_tambon', true);
      unset($tambonArray);
      unset($data);
      fclose($handle);
    }

    //Init village data
    $file = "modules/VoteData/data/village.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $villageArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $villageArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'moo'       => $data[4],
                                      'name'      => $data[5],
                                      'latitude'  => $data[6],
                                      'longitude' => $data[7]
                                    );
      }
      DBUtil::insertObjectArray($villageArray, 'votedata_village', true);
      unset($villageArray);
      unset($data);
      fclose($handle);
    }

    //Init house no. data
    $file = "modules/VoteData/data/house_no.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $houseNoArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $houseNoArray[] = array(
                                      'id'        => $data[0],
                                      'pro_id'    => $data[1],
                                      'dis_id'    => $data[2],
                                      'tam_id'    => $data[3],
                                      'vil_id'    => $data[4],
                                      'vil_moo'   => $data[5],
                                      'unit'      => $data[6],
                                      'no'        => $data[7],
                                      'trok'      => $data[8],
                                      'soi'       => $data[9],
                                      'road'      => $data[10],
                                      'zipcode'   => $data[11],
                                      'latitude'  => $data[12],
                                      'longitude' => $data[13]
                                    );
      }
      DBUtil::insertObjectArray($houseNoArray, 'votedata_house_no', true);
      unset($houseNoArray);
      unset($data);
      fclose($handle);
    }

    //Init person data
    $file = "modules/VoteData/data/person.csv";
    $handle = fopen($file, "r");
    if($handle!==false){
      $personArray =array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $personArray[] = array(
                                      'id'          => $data[0],
                                      'pro_id'      => $data[1],
                                      'dis_id'      => $data[2],
                                      'tam_id'      => $data[3],
                                      'vil_id'      => $data[4],
                                      'vil_moo'     => $data[5],
                                      'hou_id'      => $data[6],
                                      'gen_id'      => $data[7],
                                      'pid'         => $data[8],
                                      'firstname'   => $data[9],
                                      'lastname'    => $data[10],
                                      'telephone'   => $data[11],
                                      'mobilephone' => $data[12],
                                      'birthdate'   => $data[13]
                                    );
      }
      DBUtil::insertObjectArray($personArray, 'votedata_person', true);
      unset($personArray);
      unset($data);
      fclose($handle);
    }

  }

  function VoteData_fixEncoding($in_str) {
    $cur_encoding = mb_detect_encoding($in_str) ;

    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
      return $in_str;
    else
      return utf8_encode($in_str);
  } 

